/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.commonres.widget

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.method.DigitsKeyListener
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.View
import android.widget.*
import androidx.constraintlayout.widget.Guideline
import com.climber.android.commonres.helper.onClick
import com.climber.android.commonres.R

/**
 * Created by mikes on 2018/5/3.
 */
class CommonInputContentView : LinearLayout {
    private lateinit var tv_left: TextView
    private lateinit var iv_left: ImageView
    private lateinit var et_input: EditText
    private lateinit var leftLayout : FrameLayout

    private lateinit var passwordVisibility : View

    private lateinit var iv_password_eye_off : ImageView
    private lateinit var iv_password_eye_on : ImageView

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet) {
        View.inflate(context, R.layout.com_view_input_password, this)

        orientation = VERTICAL

        passwordVisibility = findViewById(R.id.passwordVisibility)

        leftLayout = findViewById(R.id.leftLayout)
        tv_left = findViewById(R.id.tv_left)
        iv_left = findViewById(R.id.iv_left)
        et_input = findViewById(R.id.et_input)

        iv_password_eye_off = findViewById(R.id.iv_password_eye_off)
        iv_password_eye_on = findViewById(R.id.iv_password_eye_on)

        iv_password_eye_off.onClick{
            iv_password_eye_off.visibility = View.GONE
            iv_password_eye_on.visibility = View.VISIBLE
            et_input.transformationMethod = HideReturnsTransformationMethod.getInstance()
            et_input.setSelection(et_input.text.length)
        }

        iv_password_eye_on.onClick{
            iv_password_eye_off.visibility = View.VISIBLE
            iv_password_eye_on.visibility = View.GONE
            et_input.transformationMethod = PasswordTransformationMethod.getInstance()
        }

        val guideLine = findViewById<Guideline>(R.id.guideLine)

        val a = context.obtainStyledAttributes(attrs, R.styleable.CommonInputContentView)

        a.getString(R.styleable.CommonInputContentView_ipvLeftText).run {
            if (!TextUtils.isEmpty(this)) {
                tv_left.text = this
            } else{
                tv_left.visibility = View.GONE
            }
        }

        a.getResourceId(R.styleable.CommonInputContentView_ipvLeftSrc, 0).run {
            if(this > 0){
                iv_left.setImageResource(this)
            } else{
                iv_left.visibility = View.GONE
            }
        }

        a.getString(R.styleable.CommonInputContentView_ipvPasswordHint).run {
            if (!TextUtils.isEmpty(this)) {
                et_input.hint = this
            }
        }

        a.getInt(R.styleable.CommonInputContentView_ipvPasswordType, 0).run {
            when(this){
                0 -> {
                    passwordVisibility.visibility = View.VISIBLE
                    // 登录密码
                    et_input.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                }
                1 -> {
                    passwordVisibility.visibility = View.VISIBLE
                    // 支付密码
                    et_input.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
                    et_input.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(6))
                    // 设置只接受数字
                    et_input.keyListener = DigitsKeyListener.getInstance("0123456789")
                }
                2 -> {
                    passwordVisibility.visibility = View.GONE
                    // 电话号码
                    et_input.inputType = InputType.TYPE_CLASS_PHONE
                    // 设置只接受数字
                    et_input.keyListener = DigitsKeyListener.getInstance("0123456789")
                }
                3 ->{//普通文本
                    passwordVisibility.visibility = View.GONE
                    et_input.inputType = InputType.TYPE_CLASS_TEXT
                }
            }
        }

        a.getInt(R.styleable.CommonInputContentView_ipvPasswordState, 0).run {
            if(this == 1){
                et_input.isEnabled = false
            }
        }

        a.getFloat(R.styleable.CommonInputContentView_ipvGuideLinePercent, 0.2f).run {
            guideLine.setGuidelinePercent(this)
        }

        a.recycle()
    }

    fun updateItem(leftText: String? = null, rightHint: String? = null) {
        if (leftText != null) {
            tv_left.text = leftText
        }
        if (rightHint != null) {
            et_input.hint = rightHint
        }
    }

    fun getInputContent() : String = et_input.text.toString().trim()

    fun getInputView() : EditText = et_input

    fun registerLeftViewClick(handler : (v : android.view.View?)-> Unit){
        leftLayout.onClick(handler = handler)
    }

    fun getLeftTextView() : TextView = tv_left

    fun updateLeftContent(content : String){
        tv_left.text = content
    }
}